<template>
  <div class="forgot-pwd">
    <div class="steps">
      <div class="header">
        <div class="logo">
          <img :src="logo" alt="" />
        </div>
        <div class="title">重置密码</div>
      </div>
      <el-steps :active="active" simple finish-status="success">
        <el-step title="验证手机号"></el-step>
        <el-step title="重置密码"></el-step>
      </el-steps>
    </div>
    <div class="content">
      <div class="verify-phone" v-show="active === 0">
        <el-form :model="form" ref="form" :rules="rules">
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入账号"
              :clearable="true"
              auto-complete="off"
              prefix-icon="el-icon-user"
            ></el-input>
          </el-form-item>
          <el-form-item prop="phone">
            <el-input
              v-model="form.phone"
              placeholder="请输入手机号"
              :clearable="true"
              auto-complete="off"
              prefix-icon="el-icon-phone"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div
              class="btn"
              v-loading="verifyLoading"
              element-loading-spinner="el-icon-loading"
              @click="verifyPhone"
            >
              验证手机号
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="reset-pwd" v-show="active === 1">
        <el-form :model="form" ref="pwdForm" :rules="rules">
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              type="password"
              :clearable="true"
              show-password
              auto-complete="off"
              placeholder="请输入新密码"
              prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <div
              class="btn"
              v-loading="resetPwdLoading"
              element-loading-spinner="el-icon-loading"
              @click="resetPwd"
            >
              重置密码
            </div>
          </el-form-item>
        </el-form>
      </div>
      <div class="success" v-show="active === 2">
        <el-result icon="success" title="重置成功">
          <template slot="subTitle">
            <p>请使用新密码登录</p>
          </template>
          <template slot="extra">
            <el-button
              type="primary"
              size="medium"
              @click="$router.push('/login')"
              >返回登录</el-button
            >
          </template>
        </el-result>
      </div>
    </div>
  </div>
</template>

<script>
import { resetPassword, verifyPhoen } from "@/api/user/user";
import logo from "@/assets/logo.jpg";
export default {
  name: "ForgotView",
  data() {
    return {
      logo,
      active: 0,
      verifyLoading: false,
      resetPwdLoading: false,
      form: {
        username: "",
        phone: "",
        password: "",
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        phone: [
          { required: true, message: "请输入手机号", trigger: "blur" },
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入新密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // 验证手机号
    verifyPhone() {
      this.$refs.form.validate(async (v) => {
        if (v) {
          try {
            this.verifyLoading = true;
            const res = await verifyPhoen(this.form);
            if (res.data) {
              this.active = 1;
            }
          } finally {
            this.verifyLoading = false;
          }
        }
      });
    },
    // 重置密码
    resetPwd() {
      this.$refs.pwdForm.validate(async (v) => {
        if (v) {
          try {
            this.resetPwdLoading = true;
            const res = await resetPassword(this.form);
            this.active = 2;
          } finally {
            this.resetPwdLoading = false;
          }
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
.forgot-pwd {
  margin: 60px auto;
  width: 80%;
  .steps {
    margin: 0 auto;
    width: 800px;
    .header {
      .logo {
        margin: 0 auto;
        width: 150px;
        height: 150px;
        border-radius: 50%;
        img {
          width: 100%;
          height: 100%;
          border-radius: inherit;
        }
      }
      .title {
        margin-bottom: 20px;
        font-size: 20px;
        letter-spacing: 2px;
        font-weight: bold;
      }
    }
  }
  .content {
    .verify-phone,
    .reset-pwd {
      margin: 50px auto;
      width: 400px;
      .btn {
        width: 100%;
        color: #fff;
        background-color: rgba(254, 115, 0, 0.8);
        cursor: pointer;
      }
    }
  }
}
</style>